package com.shizhanzhe.gxb.brand.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.WXLoginActivity;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.brand.adapter.ThemeListAdapter;
import com.shizhanzhe.gxb.brand.bean.ThemeList;
import com.shizhanzhe.gxb.home.activity.PDDDetailActivity;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 *
 * @author zz9527
 * @date 2019/1/9
 */
public class ThemeListActivity extends SwipeBackActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.web_title)
    TextView title;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.nodata)
    ImageView nodata;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    View statusBarView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_themelist);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarView(R.id.bar_0_view)
                .init();
        title.setText(getIntent().getStringExtra("title"));
        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                list.clear();
                getData();
                refreshLayout.finishRefresh(2000);
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (UserBean.uid > 0) {
                    startActivityForResult(new Intent(ThemeListActivity.this, PDDDetailActivity.class).putExtra("goods_id", list.get(i).getGoods_id()), 120);
                } else {
                    startActivity(new Intent(ThemeListActivity.this, WXLoginActivity.class));
                }
            }
        });
        getData();
    }

    private List<ThemeList.DataBean> list = new ArrayList<>();

    private void getData() {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.THEMELIST(getIntent().getIntExtra("themeid", 0)), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                try {
                    Gson gson = new Gson();
                    list = gson.fromJson(json, ThemeList.class).getData();
                    lv.setEmptyView(nodata);
                    lv.setAdapter(new ThemeListAdapter(ThemeListActivity.this, list));
                } catch (Exception e) {
                    Toast.makeText(ThemeListActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                    Log.i("______", e.toString());
                }
            }
        });
    }

    @OnClick(R.id.backarea)
    public void onViewClicked() {
        finish();
    }
}
